<template>
  <div class="my-container">
    <!-- 已登录 -->
    <div v-if="userinfo" class="header user-info">
      <div class="base-info">
        <div class="left">
          <van-image round
              class="avatar"
              :src="userInfo.photo" fit="cover"
          />
          <span class="name">{{this.$store.state.userinfo.name}}</span>
        </div>
        <div class="right">
          <van-button size="mini" round to="/user/profile">编辑资料</van-button>
        </div>
      </div>

    </div>
    <!-- 未登录 -->
    <div v-else class="header not-login">
      <div class="login-png" @click="$router.push('/login')">
        <img class="mobile-png" src="@/assets/mobile.png" alt="">
        <span class="text" >登录/注册</span>
      </div>
    </div>

    <!-- 导航宫格 -->
    <van-grid class="grid-nav" column-num="2" clickable v-show="userinfo">
      <van-grid-item v-show="userinfo.identity === '用户'" class="grid-item" to="/home">
        <i slot="icon"   class="toutiao toutiao-shoucang"></i>
        <span slot="text" class="text">挂号</span>
      </van-grid-item>
      <van-grid-item v-show="userinfo.identity === '管理员'" class="grid-item" to="/manage">
        <i slot="icon" class="toutiao toutiao-shoucang"></i>
        <span slot="text" class="text" >挂号管理</span>
      </van-grid-item>
      <van-grid-item class="grid-item"  >
        <i slot="icon" class="toutiao toutiao-lishi"></i>
        <span slot="text" class="text">历史</span>
      </van-grid-item>
    </van-grid>
    <van-cell title="消息通知" is-link v-show="userinfo"/>
    <van-cell class="md-9" title="小医同学" is-link  />
    <van-cell class="md-9" v-show="userinfo.identity === '用户'" title="挂号页面" is-link  to="/home"/>
    <van-cell v-if="userinfo" class="logout-cell" title="退出登录" @click="onlogout" clickable />
  </div>
</template>

<script>
import { mapState } from 'vuex'
// import { getUserInfo } from '@/api/user'
export default {
  name: 'MyIndex',
  data () {
    return {
      userInfo: {} // 用户信息
    }
  },
  created () {
    // this.userinfo = ''
    if (this.userinfo) {
      // 如果用户登录则请求加载用户信息
      // this.loadUserInfo()
    }
  },
  methods: {
    onlogout () {
      // 1.退出提示
      // 在组件中要用this.$dialog来调用弹窗
      this.$dialog.confirm({
        title: '确认退出吗？'
      })
        .then(() => {
          // on confirm
          // 成功执行
          // 2.退出清除user和本地的token
          // vuex里面的setuser就是管理user的值的，所以直接传一个空值即可
          this.$store.commit('setUser', '')
          this.$router.push('/login')
          this.$store.commit('clearWaitName', null)
          this.$store.commit('clearwaitPeopleList', null)
        })
        .catch(() => {
          // 失败执行
          // on cancel
        })
    }

  },
  computed: {
    ...mapState(['userinfo'])
  }
}
</script>

<style lang="less" scoped>
.my-container {
  .header {
    height: 175px;
   background: url('@/assets/banner.png');
   background-size: cover;

  }
  .not-login {
      display: flex;
      justify-content: center;
      align-items: center;
      .login-png {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
      .mobile-png {
        width: 68px;
        height: 68px;
      }
      .text {
        padding-top: 10px;
        font-size: 20x;
        color: black;
      }
      }
  }
  .user-info {
    .base-info {
      height: 80px;

      box-sizing: border-box;
      padding: 26px 16px 15px;
      padding-top: 55px;
      display: flex;
      justify-content: space-between;
      align-content: center;
      .left {
        display: flex;
        align-content: center;
        .avatar {
          width: 66px;
          height: 66px;
          margin-right: 23px;
          border: 1px solid #fff;
        }
        .name {
          font-size: 15px;
          color: #fff;

        }
      }
    }

  }
  .grid-nav {
    height: 100px;
    .grid-item {
      i.toutiao {
        font-size: 30px;
      }
      .toutiao-shoucang {
        color: #eb5253;
      }
      .toutiao-lishi {
        color: #ff9d1d;
      }
      span.text {
        font-size: 15px;
      }
    }
  }
  .logout-cell {
    background-color: aliceblue;
    text-align: center;
    color: orange;
  }
}
</style>
